pub mod doudian {
    use chromiumoxide::{Element, Page};

    use crate::fnutils::fnutils::wait_fn;


    pub async fn wait_for_elem(page: Page, css: &str, wait_ms: u32) -> Element {
        let find_elem = |page: &Page| -> Result<Element, String> {
            let fut = page.find_element(css);
            futures::executor::block_on(fut).map_err(|error| error.to_string())
        };
        let until_fn = |result: &Result<Element, String>| {
            result.is_ok()
        };
        let wait = wait_fn(find_elem, until_fn, wait_ms, 300);
        async {
            wait(page).unwrap()
        }.await
    }

    #[cfg(test)]
    mod test {
        use crate::{doudian::doudian::wait_for_elem, spider::{self, init_page}};
        use anyhow::Result;

        #[tokio::test]
        async fn enter_home() -> Result<()> {
            let (browser, handle) = spider::launch_browser().await?;
            let page = browser.new_page("https://fxg.jinritemai.com/ffa/mshop/homepage/index").await?;

            page.evaluate_expression(
                r#"Object.defineProperty(navigator,"webdriver",{get:()=>undefined})"#,
            )
            .await?;
            page.evaluate_expression(r#"window.navigator.chrome = { runtime: {} }"#)
                .await?;
            page.evaluate_expression(
                r#"Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] })"#,
            )
            .await?;
            page.evaluate_expression(
                r#"Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3, 4, 5,6], })"#,
            )
            .await?;

            init_page(&page).await?;

            let elem = wait_for_elem(page, "div.style_left-side-bar__2OzUz", 60*1000).await;
            println!("{}", elem.inner_html().await?.unwrap());

            spider::close_browser(browser, handle).await?;
            Ok(())
        }
    }
}